{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data size 1903073\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python3\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "import json\n",
    "import logging\n",
    "import os\n",
    "import numpy as np\n",
    "\n",
    "import tensorflow as tf\n",
    "\n",
    "import utils\n",
    "from model import Model\n",
    "from utils import read_data\n",
    "\n",
    "from flags import parse_args\n",
    "FLAGS, unparsed = parse_args()\n",
    "\n",
    "\n",
    "logging.basicConfig(\n",
    "    format='%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s', level=logging.DEBUG)\n",
    "\n",
    "\n",
    "vocabulary = read_data('QuanSongCi.txt')\n",
    "print('Data size', len(vocabulary))\n",
    "\n",
    "with open('dictionary.json', encoding='utf-8') as inf:\n",
    "    dictionary = json.load(inf, encoding='utf-8')\n",
    "\n",
    "with open('reverse_dictionary.json', encoding='utf-8') as inf:\n",
    "    reverse_dictionary = json.load(inf, encoding='utf-8')\n",
    "\n",
    "data=[]\n",
    "for word in vocabulary:\n",
    "    index = dictionary.get(word)\n",
    "    if index==None:\n",
    "        index=0\n",
    "    data.append(index)\n",
    "\n",
    "model = Model()\n",
    "model.build()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-05-23 16:05:22,119 - DEBUG - <ipython-input-2-fdaa9c634529>:7 - Initialized\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from ./rnn_log\\model.ckpt-1370\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-05-23 16:05:22,123 - INFO - tf_logging.py:82 - Restoring parameters from ./rnn_log\\model.ckpt-1370\n",
      "2018-05-23 16:05:22,169 - DEBUG - <ipython-input-2-fdaa9c634529>:12 - restore from [./rnn_log\\model.ckpt-1370]\n",
      "2018-05-23 16:05:22,169 - DEBUG - <ipython-input-2-fdaa9c634529>:18 - epoch [0]....\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred0 [5.5025751e-04 8.1135675e-02 4.7921503e-04 ... 4.1504015e-07 2.4750350e-05\n",
      " 4.3884785e-07]\n",
      "pred0 1.000004\n",
      "predmax 1\n",
      "pred0 [4.7775838e-04 5.7775538e-02 4.5764813e-04 ... 2.5255440e-07 1.9559177e-05\n",
      " 2.6993874e-07]\n",
      "pred0 1.0000085\n",
      "predmax 3\n",
      "pred0 [3.7293060e-04 1.9835213e-01 3.4917981e-04 ... 2.2472786e-07 1.7951468e-05\n",
      " 2.3889737e-07]\n",
      "pred0 1.0000167\n",
      "predmax 1\n",
      "pred0 [5.6005549e-04 6.2819779e-02 3.5700324e-04 ... 4.4141456e-07 2.6764914e-05\n",
      " 4.5234117e-07]\n",
      "pred0 0.9999995\n",
      "predmax 1\n",
      "pred0 [3.1121421e-04 1.6799952e-01 8.3850576e-03 ... 5.9395044e-09 1.3324856e-06\n",
      " 6.3107986e-09]\n",
      "pred0 1.0000095\n",
      "predmax 3\n",
      "pred0 [1.00134006e-04 5.41024923e-01 4.44942852e-03 ... 1.25080679e-09\n",
      " 4.49811694e-07 1.39542367e-09]\n",
      "pred0 1.0000048\n",
      "predmax 1\n",
      "pred0 [5.6921248e-04 7.0024297e-02 5.4587802e-04 ... 6.2871374e-07 3.1836440e-05\n",
      " 6.4204528e-07]\n",
      "pred0 0.9999988\n",
      "predmax 10\n",
      "pred0 [1.2876425e-03 8.0779946e-04 5.5420257e-02 ... 3.7612187e-07 2.2342886e-06\n",
      " 3.7378544e-07]\n",
      "pred0 1.0000031\n",
      "predmax 440\n",
      "pred0 [1.3020540e-03 8.2127907e-04 5.7170149e-02 ... 3.6092104e-07 2.1571846e-06\n",
      " 3.5867902e-07]\n",
      "pred0 1.000008\n",
      "predmax 2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-05-23 16:05:24,088 - DEBUG - <ipython-input-2-fdaa9c634529>:38 - step [1380] loss [6.41052770614624]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred0 [6.9229805e-04 1.5117626e-03 1.1899387e-03 ... 5.7207450e-07 1.5946553e-05\n",
      " 5.6940252e-07]\n",
      "pred0 0.9999968\n",
      "predmax 18\n",
      "pred0 [8.56126717e-04 1.54946267e-03 2.59800814e-03 ... 5.79723746e-07\n",
      " 1.22833435e-05 5.93700236e-07]\n",
      "pred0 0.9999968\n",
      "predmax 18\n",
      "pred0 [5.9229863e-04 1.1753555e-02 1.4938732e-03 ... 7.0370874e-08 6.2341696e-06\n",
      " 7.1574959e-08]\n",
      "pred0 1.0000074\n",
      "predmax 3\n",
      "pred0 [1.0824727e-03 3.2314144e-03 4.9031512e-03 ... 3.7795647e-07 6.9674038e-06\n",
      " 3.8157199e-07]\n",
      "pred0 0.9999911\n",
      "predmax 12\n",
      "pred0 [5.8827130e-04 2.9731335e-02 5.2815973e-04 ... 1.8198412e-07 1.2707981e-05\n",
      " 1.8988688e-07]\n",
      "pred0 1.0000174\n",
      "predmax 3\n",
      "pred0 [7.2853721e-04 1.7928362e-02 1.2196139e-03 ... 2.3139452e-07 1.2238431e-05\n",
      " 2.3932839e-07]\n",
      "pred0 1.0000063\n",
      "predmax 3\n",
      "pred0 [6.3693023e-04 2.3175885e-03 1.3772412e-03 ... 1.7279275e-07 7.9048114e-06\n",
      " 1.7364616e-07]\n",
      "pred0 0.9999899\n",
      "predmax 22\n",
      "pred0 [5.01328206e-04 1.09915614e-01 1.22107286e-02 ... 1.70109971e-08\n",
      " 2.53966869e-06 1.81370421e-08]\n",
      "pred0 1.0000222\n",
      "predmax 3\n",
      "pred0 [9.8668039e-04 1.7752965e-03 3.7033516e-03 ... 6.7050485e-07 1.3092465e-05\n",
      " 6.4766863e-07]\n",
      "pred0 0.999999\n",
      "predmax 12\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-05-23 16:05:26,579 - DEBUG - <ipython-input-2-fdaa9c634529>:38 - step [1390] loss [6.281939506530762]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred0 [4.5117256e-04 7.0534788e-02 4.0058949e-04 ... 2.2251434e-07 1.9018782e-05\n",
      " 2.4475435e-07]\n",
      "pred0 1.0000186\n",
      "predmax 3\n",
      "pred0 [7.0507638e-05 6.4900941e-01 4.4272438e-04 ... 1.6841655e-09 7.3126398e-07\n",
      " 1.8990050e-09]\n",
      "pred0 1.0000051\n",
      "predmax 1\n",
      "pred0 [4.6074681e-04 2.1081224e-02 7.8443045e-05 ... 1.9266231e-06 5.6551831e-05\n",
      " 2.0317859e-06]\n",
      "pred0 0.9999982\n",
      "predmax 1\n",
      "pred0 [3.5651829e-04 2.2323191e-01 3.6302858e-04 ... 8.9224770e-08 1.0048913e-05\n",
      " 9.6479219e-08]\n",
      "pred0 0.9999745\n",
      "predmax 1\n",
      "pred0 [1.1264333e-03 1.4294054e-03 9.9279936e-03 ... 1.0651945e-06 1.2986592e-05\n",
      " 1.0766781e-06]\n",
      "pred0 0.99999756\n",
      "predmax 12\n",
      "pred0 [4.6757123e-04 1.3124983e-01 8.2886411e-04 ... 7.2926127e-08 7.2901857e-06\n",
      " 7.9726654e-08]\n",
      "pred0 1.0000117\n",
      "predmax 3\n",
      "pred0 [7.7610405e-04 3.9717355e-03 3.8990146e-03 ... 9.6201234e-08 4.6575947e-06\n",
      " 9.9719223e-08]\n",
      "pred0 1.0000235\n",
      "predmax 18\n",
      "pred0 [5.0024147e-04 3.9877478e-02 9.8123238e-04 ... 4.2581629e-08 5.1610778e-06\n",
      " 4.4622016e-08]\n",
      "pred0 0.9999658\n",
      "predmax 3\n",
      "pred0 [5.4019468e-04 3.8602117e-02 1.3561479e-03 ... 1.2312530e-07 1.1029873e-05\n",
      " 1.2722819e-07]\n",
      "pred0 1.0000122\n",
      "predmax 3\n",
      "pred0 [1.2156050e-05 4.7469995e-04 9.7988850e-01 ... 4.4499926e-13 6.6003703e-11\n",
      " 4.3885037e-13]\n",
      "pred0 1.0000091\n",
      "predmax 2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-05-23 16:05:29,054 - DEBUG - <ipython-input-2-fdaa9c634529>:38 - step [1400] loss [6.613417148590088]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred0 [5.3641648e-04 6.7855574e-02 3.3343213e-03 ... 2.5711696e-08 2.7350336e-06\n",
      " 2.8160944e-08]\n",
      "pred0 1.0000349\n",
      "predmax 3\n",
      "pred0 [8.8129914e-04 1.4138907e-02 9.9424068e-03 ... 1.5828972e-07 6.3758130e-06\n",
      " 1.5836612e-07]\n",
      "pred0 1.0000092\n",
      "predmax 9\n",
      "pred0 [6.2130863e-04 7.0874706e-02 6.8373131e-03 ... 3.7487283e-08 2.9485373e-06\n",
      " 4.0378168e-08]\n",
      "pred0 1.0000517\n",
      "predmax 3\n",
      "pred0 [7.8515668e-04 4.8935134e-03 1.6396990e-03 ... 9.6737040e-07 2.0088042e-05\n",
      " 9.6490203e-07]\n",
      "pred0 1.0000004\n",
      "predmax 206\n",
      "pred0 [6.4787740e-04 1.3044745e-03 9.0885471e-04 ... 5.3213972e-07 1.5411057e-05\n",
      " 5.3389601e-07]\n",
      "pred0 1.0000006\n",
      "predmax 18\n",
      "pred0 [1.0320190e-03 2.5693823e-03 3.1313592e-01 ... 1.3844620e-07 1.5403609e-06\n",
      " 1.3963560e-07]\n",
      "pred0 0.9999805\n",
      "predmax 2\n",
      "pred0 [8.6070120e-04 1.4617610e-03 4.8290336e-01 ... 9.3145395e-08 8.9649313e-07\n",
      " 9.2580379e-08]\n",
      "pred0 0.9999657\n",
      "predmax 2\n",
      "pred0 [8.8634703e-04 1.0431423e-02 5.8709219e-04 ... 8.0742882e-07 1.8507744e-05\n",
      " 8.3832572e-07]\n",
      "pred0 1.0000001\n",
      "predmax 3\n",
      "pred0 [6.8132416e-04 6.9752537e-02 1.5264944e-03 ... 3.2617027e-07 1.7002485e-05\n",
      " 3.3899073e-07]\n",
      "pred0 1.0000042\n",
      "predmax 3\n",
      "pred0 [1.5092019e-03 7.4647850e-04 6.7302279e-02 ... 2.7501372e-07 1.6185683e-06\n",
      " 2.7237982e-07]\n",
      "pred0 0.9999996\n",
      "predmax 2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-05-23 16:05:31,527 - DEBUG - <ipython-input-2-fdaa9c634529>:38 - step [1410] loss [6.677258491516113]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred0 [6.5138302e-04 1.5261867e-03 4.2658174e-04 ... 6.6352050e-07 1.8965064e-05\n",
      " 6.3892406e-07]\n",
      "pred0 1.0000015\n",
      "predmax 18\n",
      "pred0 [6.4601284e-04 2.0488794e-03 2.8978920e-04 ... 1.3111026e-06 2.9335590e-05\n",
      " 1.3226905e-06]\n",
      "pred0 0.99999654\n",
      "predmax 18\n",
      "pred0 [5.2198675e-04 5.3185928e-03 1.1294559e-04 ... 1.9893896e-06 4.8163605e-05\n",
      " 1.9428496e-06]\n",
      "pred0 0.99999785\n",
      "predmax 61\n",
      "pred0 [5.4594874e-04 4.4149077e-03 1.8834262e-04 ... 2.5969311e-07 1.6242877e-05\n",
      " 2.5719294e-07]\n",
      "pred0 1.0000052\n",
      "predmax 18\n",
      "pred0 [1.5770023e-04 6.3728511e-01 8.3677395e-04 ... 7.0026109e-09 1.7952347e-06\n",
      " 7.6263706e-09]\n",
      "pred0 1.0000083\n",
      "predmax 1\n",
      "pred0 [5.2992988e-04 6.7707938e-03 2.4084821e-04 ... 2.7867469e-07 1.7128290e-05\n",
      " 2.7112458e-07]\n",
      "pred0 1.0000188\n",
      "predmax 46\n",
      "pred0 [1.3663288e-03 2.3218852e-03 9.7868424e-03 ... 7.5580266e-07 8.4784851e-06\n",
      " 7.4218997e-07]\n",
      "pred0 1.0000004\n",
      "predmax 12\n",
      "pred0 [5.3503650e-04 3.6521619e-03 2.4739813e-04 ... 4.0579690e-07 1.9803612e-05\n",
      " 3.9398685e-07]\n",
      "pred0 1.0000014\n",
      "predmax 46\n",
      "pred0 [1.3611455e-03 1.4799685e-03 8.6579341e-03 ... 6.4581837e-07 8.3190316e-06\n",
      " 6.4281306e-07]\n",
      "pred0 0.9999993\n",
      "predmax 12\n",
      "pred0 [8.1287889e-04 2.0266186e-02 1.8890331e-02 ... 1.1801864e-07 5.7022380e-06\n",
      " 1.1806299e-07]\n",
      "pred0 1.0000265\n",
      "predmax 9\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2018-05-23 16:05:33,880 - DEBUG - <ipython-input-2-fdaa9c634529>:38 - step [1420] loss [6.627198219299316]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pred0 [3.8726773e-04 1.7923543e-01 1.3137482e-03 ... 3.9856495e-08 6.2271088e-06\n",
      " 4.3518391e-08]\n",
      "pred0 0.99997014\n",
      "predmax 3\n",
      "pred0 [5.9550779e-04 3.1267947e-03 4.2575036e-04 ... 5.2994437e-07 1.9053597e-05\n",
      " 5.2458370e-07]\n",
      "pred0 0.999999\n",
      "predmax 206\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-fdaa9c634529>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     28\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     29\u001b[0m             pred,gs, _, state, l, summary_string = sess.run(\n\u001b[1;32m---> 30\u001b[1;33m                 [model.predictions,model.global_step, model.optimizer, model.outputs_state_tensor, model.loss, model.merged_summary_op], feed_dict=feed_dict)\n\u001b[0m\u001b[0;32m     31\u001b[0m             \u001b[0msummary_string_writer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_summary\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msummary_string\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     32\u001b[0m             \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'pred0'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mpred\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m    787\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    788\u001b[0m       result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[1;32m--> 789\u001b[1;33m                          run_metadata_ptr)\n\u001b[0m\u001b[0;32m    790\u001b[0m       \u001b[1;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    791\u001b[0m         \u001b[0mproto_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run\u001b[1;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m    995\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    996\u001b[0m       results = self._do_run(handle, final_targets, final_fetches,\n\u001b[1;32m--> 997\u001b[1;33m                              feed_dict_string, options, run_metadata)\n\u001b[0m\u001b[0;32m    998\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    999\u001b[0m       \u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_run\u001b[1;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m   1130\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1131\u001b[0m       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,\n\u001b[1;32m-> 1132\u001b[1;33m                            target_list, options, run_metadata)\n\u001b[0m\u001b[0;32m   1133\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1134\u001b[0m       return self._do_call(_prun_fn, self._session, handle, feed_dict,\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_call\u001b[1;34m(self, fn, *args)\u001b[0m\n\u001b[0;32m   1137\u001b[0m   \u001b[1;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1138\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1139\u001b[1;33m       \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1140\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1141\u001b[0m       \u001b[0mmessage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[1;34m(session, feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[0;32m   1119\u001b[0m         return tf_session.TF_Run(session, options,\n\u001b[0;32m   1120\u001b[0m                                  \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1121\u001b[1;33m                                  status, run_metadata)\n\u001b[0m\u001b[0;32m   1122\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1123\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msession\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "with tf.Session() as sess:\n",
    "    summary_string_writer = tf.summary.FileWriter(FLAGS.output_dir, sess.graph)\n",
    "\n",
    "    saver = tf.train.Saver(max_to_keep=5)\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    sess.run(tf.local_variables_initializer())\n",
    "    logging.debug('Initialized')\n",
    "\n",
    "    try:\n",
    "        checkpoint_path = tf.train.latest_checkpoint(FLAGS.output_dir)\n",
    "        saver.restore(sess, checkpoint_path)\n",
    "        logging.debug('restore from [{0}]'.format(checkpoint_path))\n",
    "\n",
    "    except Exception:\n",
    "        logging.debug('no check point found....')\n",
    "\n",
    "    for x in range(1):\n",
    "        logging.debug('epoch [{0}]....'.format(x))\n",
    "        state = sess.run(model.state_tensor)\n",
    "        for dl in utils.get_train_data(vocabulary, batch_size=16, num_steps=32):\n",
    "\n",
    "            ##################\n",
    "            X = utils.index_data(dl[0], dictionary)\n",
    "            Y = utils.index_data(dl[1], dictionary)\n",
    "            feed_dict={ model.X:X,model.Y: Y,model.state_tensor: state}\n",
    "            \n",
    "            ##################\n",
    "\n",
    "            pred,gs, _, state, l, summary_string = sess.run(\n",
    "                [model.predictions,model.global_step, model.optimizer, model.outputs_state_tensor, model.loss, model.merged_summary_op], feed_dict=feed_dict)\n",
    "            summary_string_writer.add_summary(summary_string, gs)\n",
    "            print('pred0',pred[0][-1])\n",
    "            print('pred0',np.sum(pred[0][-1]))\n",
    "            print('predmax',np.argmax(pred[0][-1]))\n",
    "            \n",
    "            \n",
    "            if gs % 10 == 0:\n",
    "                logging.debug('step [{0}] loss [{1}]'.format(gs, l))\n",
    "                save_path = saver.save(sess, os.path.join(\n",
    "                    FLAGS.output_dir, \"model.ckpt\"), global_step=gs)\n",
    "    summary_string_writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "pred[0].argsort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "a=np.array([1,3,2,7,4,3])\n",
    "a.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
